import waterfall from 'async/waterfall'
import { webapp_ws_ajax_run, webapp_ws_run } from './grwebapp'
import qs from 'qs'
function print(data, vm) {
  let _task = []
  data.forEach((item) => {
    _task.push((done) => {
      run(item.type, item.reportUrl, item.Table, item.Copies, item.PrinterName, item.PaperName, vm)
      setTimeout(() => {
        done()
      }, 1000)
    })
  })

  waterfall(_task, function (err, result) {
    console.log('完成 print', err, result)
  })
}
function newRun(type, url) {
  console.log('url', url)
  webapp_ws_run(
    { type: type || 'preview' }, //设置不同的属性可以执行不同的任务，如：preview print pdf xls csv txt rtf img grd
    url,
    ''
  )
}
function run(type, url, Table, Copies, PrinterName, PaperName, vm) {
  console.log('测试打印1', { type, url, Table, Copies, PrinterName, PaperName, vm })

  let _data = {
    method: 'wms.file.common.download',
    token: vm.$store.getters['user/token'],
  }
  let _jsonData = JSON.stringify({
    '#OUTPUT_TYPE#': 'stream',
    FILE_SEQ: url,
  })
  console.log('测试打印2', _data)
  let _url = ``

  if (!isNaN(Number(url))) {
    _url = `${location.origin}/wms/wms-web?${qs.stringify(_data)}&data=${encodeURIComponent(_jsonData)}`
    // _url = `http://192.168.3.211/wms/wms-web?${qs.stringify(_data)}&data=${encodeURIComponent(_jsonData)}`
  } else {
    _url = url
  }
  console.log('测试打印3', _url)
  // webapp_ws_run({ type: type || 'preview' },)
  webapp_ws_ajax_run({
    type: type || 'preview', //设置不同的属性可以执行不同的任务，如：preview print pdf xls csv txt rtf img grd
    report: _url, //urlAddRandomNo(_url), //"http://localhost:8000/1a.grf"
    data: {
      Table: Table,
    },
    PrinterName: PrinterName,
    PaperName: PaperName,
    Copies: Copies || '1',
    // data: {
    //   Table: [
    //     {
    //       CustomerID: "HUNGC",
    //       CompanyName: "五金机械",
    //       ContactName: "苏先生",
    //       ContactTitle: "销售代表",
    //       Address: "德昌路甲 29 号",
    //       City: "大连",
    //       Region: "东北",
    //       PostalCode: "564576",
    //       Country: "中国",
    //       Phone: "(053) 5556874",
    //       Fax: "(053) 5552376",
    //     },
    //   ],
    //
    // },
  })
}
export { print, run, newRun }
